package com.zyn.rsgl.controller;


import com.zyn.rsgl.pojo.Bulletin;
import com.zyn.rsgl.service.BulletinService;
import com.zyn.rsgl.utils.commonResult.R;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;

/**
 * <p>
 * 公告表 前端控制器
 * </p>
 *
 * @author zcs
 * @since 2025-02-26
 */
@RestController
@RequestMapping("/rsgl/bulletin")
public class BulletinController {
    @Resource
    private BulletinService bulletinService;

    // 查询所有
    @GetMapping("/list")
    public R list(@RequestParam(defaultValue = "1") Integer pageNum,
                  @RequestParam(defaultValue = "10") Integer pageSize,
                  @RequestParam(required = false) String title,
                  @RequestParam(required = false) String time) {
        return bulletinService.listPage(pageNum, pageSize, title, time);
    }

    // 根据ID获取详细信息
    @GetMapping("/get/{bulletinId}")
    public R get(@PathVariable("bulletinId") String bulletinId) {
        Bulletin byId = bulletinService.getById(bulletinId);
        if (byId != null) {
            return R.ok(byId);
        }
        return R.fail("未查询到该公告");
    }

    // 添加
    @PostMapping("/add")
    public R add(@RequestParam Bulletin bulletin) {
        // 判断是否存在
        if (bulletinService.getById(bulletin.getId()) != null) {
            return R.fail("该公告已存在");
        }
        return bulletinService.save(bulletin) ? R.ok() : R.fail("添加失败");
    }

    // 修改
    @PutMapping("/update")
    public R update(@RequestBody Bulletin bulletin) {
        return bulletinService.updateById(bulletin) ? R.ok() : R.fail("修改失败");
    }

    // 删除
    @DeleteMapping("/delete/{bulletinId}")
    public R delete(@PathVariable("bulletinId") String bulletinId) {
        return bulletinService.removeById(bulletinId) ? R.ok() : R.fail("删除失败");
    }
}
